﻿"""测试单选框"""
import tkinter as tk
from tkinter import messagebox
from PIL import Image, ImageTk
import random
from tkinter.filedialog import *
from tkinter.colorchooser import *


class Application(tk.Frame):  # Application是应用的含义，需要继承Frame的初始化
    """一个经典的GUI程序的类的写法"""

    def __init__(self, master=None):  # 要传递master
        super().__init__(master)
        self.master = master
        self.pack()  # 通过布局管理显示
        self.create_widget()  # 调用类函数进行初始化小部件
        self.filename = None  # 加上这个来防止报错

    def test(self):
        pass

    def create_widget(self):  # Widget是小部件的含义
        # 创建主菜单栏
        menubar = tk.Menu(self.master)
        # 创建子菜单
        menu_file = tk.Menu(menubar)
        menu_edit = tk.Menu(menubar)
        menu_help = tk.Menu(menubar)
        # 讲子菜单加入到主菜单栏
        menubar.add_cascade(label='文件(F)', menu=menu_file)
        menubar.add_cascade(label='编辑(E)', menu=menu_edit)
        menubar.add_cascade(label='帮助(H)', menu=menu_help)
        # 添加菜单项
        menu_file.add_command(label='新建', accelerator='ctrl+n', command=self.new_file)
        menu_file.add_command(label='打开', accelerator='ctrl+o', command=self.openfile)
        menu_file.add_command(label='保存', accelerator='ctrl+s', command=self.save_file)
        menu_file.add_separator()  # 添加分割线
        menu_file.add_command(label='退出', accelerator='ctrl+q', command=self.exit)
        self.master['menu'] = menubar
        # 文本编辑区
        self.textpad = tk.Text(self.master, width=50, height=30)
        self.textpad.pack()
        # 创建上下菜单
        self.contextMenu = tk.Menu(self.master)
        self.contextMenu.add_command(label='背景色', command=self.open_ask_bk_color)
        # 为右键绑定事件
        self.master.bind('<Button-3>', self.create_context_menu)
        # 快捷键绑定事件
        self.master.bind('<Control-n>', lambda event: self.new_file())
        self.master.bind('<Control-o>', lambda event: self.openfile())
        self.master.bind('<Control-s>', lambda event: self.save_file())
        self.master.bind('<Control-q>', lambda event: self.exit())

    def openfile(self):
        self.textpad.delete('1.0', 'end')  # 清空一下文本框
        with askopenfile(title='打开文本文件') as f:
            self.textpad.insert(tk.INSERT, f.read())
            self.filename = f.name

    def save_file(self):
        if not self.filename:
            self.new_file()
        with open(self.filename, 'w') as f:
            content = self.textpad.get(1.0, tk.END)
            f.write(content)

    def new_file(self):
        self.textpad.delete('1.0', 'end')  # 清空一下文本框
        self.filename = asksaveasfilename(title='另存为', initialfile='未命名.txt', filetypes=[('文本文档', '*.txt')],
                                          defaultextension='.txt')
        self.save_file()

    def exit(self):
        self.master.quit()

    def open_ask_bk_color(self):
        bk_color = askcolor(color='red', title='请选择你的背景颜色啦')
        bk_color = bk_color[1]
        self.textpad.config(bg=str(bk_color))

    def create_context_menu(self, event):
        self.contextMenu.post(event.x_root, event.y_root)


def main():
    root = tk.Tk()
    root.geometry("800x500+200+300")
    root.title("测试label标签")
    app = Application(master=root)
    root.mainloop()


if __name__ == '__main__':
    main()
